#! /usr/bin/python -W ignore
# -*- coding: utf-8 -*-
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
#
# Qview: Quick view light curves.
#
# This program NEEDS matplotlib: http://matplotlib.sourceforge.net/
#
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
import os
import sys
sys.path.append("source/")

import numpy
from pylab import *
from qviewF import *
from dataCursorC import *
from globalVariables import gv
from matplotlib.ticker import NullLocator



parseCommandLineArguments() # Parses the command line arguments
readLightCurveFile()        # Reads the data from the file



# Plots for the first time
gv.FO = figure(1)
gv.FE = figure(2)
gv.FD = figure(3)
for i in range(0,gv.ny):

  # We want to share the x axis
  # The x axis of the "raw" figure shares the x axis of the "reduced" figure
  if i==0:
    gv.SD.append( gv.FD.add_subplot( gv.ny, 1, i+1 ) )
    gv.SE.append( gv.FE.add_subplot( gv.ny, 1, i+1 ) )
    gv.SO.append( gv.FO.add_subplot( gv.ny, 1, i+1, sharex=gv.SE[0]))
  else:
    gv.SD.append( gv.FD.add_subplot(gv.ny, 1, i+1, sharex=gv.SD[0]))
    gv.SE.append( gv.FE.add_subplot(gv.ny, 1, i+1, sharex=gv.SE[0]))
    gv.SO.append( gv.FO.add_subplot(gv.ny, 1, i+1, sharex=gv.SE[0]))

  # Cursor objects
  gv.CO.append(DataCursor(1,i, gv.SO[i], useblit=True, color='red',linewidth=2))
  gv.CE.append(DataCursor(2,i, gv.SE[i], useblit=True, color='red',linewidth=2))
  gv.CD.append(DataCursor(3,i, gv.SD[i], useblit=True, color='red',linewidth=2))

  # We'll let the cursor do the toolbarformatting too.
  gv.SO[i].fmt_xdata = gv.CO[i].fmtx
  gv.SO[i].fmt_ydata = gv.CO[i].fmty
  gv.SE[i].fmt_xdata = gv.CE[i].fmtx
  gv.SE[i].fmt_ydata = gv.CE[i].fmty
  gv.SD[i].fmt_xdata = gv.CD[i].fmtx
  gv.SD[i].fmt_ydata = gv.CD[i].fmty


plotAll(plotDFT=True)



# Set the title and the hspace for the first figure
figure(1)
manager = get_current_fig_manager()
manager.window.set_title('Raw Data')
subplots_adjust(hspace=0)

# Set the title and the hspace for the second figure
figure(2)
manager = get_current_fig_manager()
manager.window.set_title('Reduced Data')
subplots_adjust(hspace=0)

# Set the title and the hspace for the third figure
figure(3)
manager = get_current_fig_manager()
manager.window.set_title('Discrete Fourier Transform')
subplots_adjust(hspace=0)




# Every time the keyboard is pressed, keyboard function is called
gv.FO.canvas.mpl_connect('key_press_event', control)
gv.FE.canvas.mpl_connect('key_press_event', control)
gv.FD.canvas.mpl_connect('key_press_event', control)


# Every time the keyboad is pressed or released, this function is called to
# set the variables gv.controlHold and gv.shiftHold
# They're used to have options like  <ctrl>+<left> etc
gv.FO.canvas.mpl_connect('key_press_event'  , shiftControlHold )
gv.FO.canvas.mpl_connect('key_release_event', shiftControlHold )
gv.FE.canvas.mpl_connect('key_press_event'  , shiftControlHold )
gv.FE.canvas.mpl_connect('key_release_event', shiftControlHold )
gv.FD.canvas.mpl_connect('key_press_event'  , shiftControlHold )
gv.FD.canvas.mpl_connect('key_release_event', shiftControlHold )



# Hereafter the function "control" controls the program wainting for
# instructions from the keyboard
# The program will finish when the user types "q"
show()
